package com.itcast.bulls.stock.starter.ceph;

import lombok.extern.log4j.Log4j2;
import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.model.Account;
import org.javaswift.joss.model.Container;
import org.javaswift.joss.model.StoredObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class CephSwiftOperator {

    private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    //用户连接名称 ：主用户名：子用户名
    private  String userName;

    private String password;

    //接口的访问地址
    private String authUrl;

    //默认存储的容器名称
    private String defaultContainerName;

    //ceph的账户操作对象
    private Account account;

    //ceph的容器操作对象
    private Container container;


    public CephSwiftOperator(String userName,String password,String authUrl,String defaultContainerName){
        this.userName=userName;
        this.password=password;
        this.authUrl=authUrl;
        this.defaultContainerName = defaultContainerName;
        init();
    }

    public void init(){
        try {
            //1.设置Ceph的账户连接信息
            AccountConfig config = new AccountConfig();
            config.setUsername(userName);
            config.setPassword(password);
            config.setAuthUrl(authUrl);
            config.setAuthenticationMethod(AuthenticationMethod.BASIC);
            account = new AccountFactory(config).createAccount();

            //2.获取容器的操作对象
            Container newContainer = account.getContainer(defaultContainerName);
            if(!newContainer.exists()){
                container = newContainer.create();
                log.info("container create =>"+defaultContainerName);
            }else {
                container = newContainer;
                log.info("container exists! =>"+defaultContainerName);

            }
        }catch (Exception e){
            log.error("Ceph连接初始化异常:"+e.getMessage());
        }
    }




    //文件上传的处理接口
    public void createObject(String remoteName,String filePath){
        //1.从容器当中获取远程存储对象信息
        StoredObject object = container.getObject(remoteName);

        //2.执行文件上传处理
        object.uploadObject(new File(filePath));
    }


    //文件上传的处理接口(字节数组形式上传）
    public void createObject(String remoteName,byte[] inputStream){
        //1.从容器当中获取远程存储对象信息
        StoredObject object = container.getObject(remoteName);

        //2.执行文件上传处理
        object.uploadObject(inputStream);
    }

    //文件下载接口
    public void retrieveObject(String objectName,String outPath){
        //1.从容器当中获取远程对象信息
        StoredObject object = container.getObject(objectName);
        //2.执行文件的下载
        object.downloadObject(new File(outPath));
    }

    //文件下载接口(转换为字节流
    public InputStream retrieveObject(String objectName){
        //1.从容器当中获取远程对象信息
        StoredObject object = container.getObject(objectName);
        //2.执行文件的下载
        return object.downloadObjectAsInputStream();
    }


    //删除文件对象
    public boolean deleteObject(String objectName){
        try {
            StoredObject object = container.getObject(objectName);
            object.delete();
            return !object.exists();
        }catch (Exception e){
            log.error("删除失败："+e.getMessage());
        }
        return false;
    }

    //获取用户下面的所有容器信息
    public List lisContainer(){
        List list = new ArrayList();
        Collection<Container> containers = account.list();
        for (Container container : containers) {
            list.add(container);
            log.info("current container name:"+container.getName());
        }
        return list;
    }

}
